package com.sduept.nwld.dataserver.controller.config;

import com.sduept.bigdata.web.core.manager.BannerManager;
import com.sduept.bigdata.web.core.model.BannerInfo;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.config.ImageManager;
import org.apache.commons.lang.StringUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Named
@ViewScoped
public class BannerManagerController extends AbstractController {
	private static final long serialVersionUID = 3864920751238738874L;

	@Autowired
	private BannerManager bm;
	@Autowired
	private ImageManager im;
//	@Autowired
//	private MobileCache mobileCache;
	
	private List<BannerInfo> banners = new ArrayList<BannerInfo>();
	private BannerInfo current;
	private TreeNode selectedUrlNode;
	
	@PostConstruct
	public void init() {
		initBannerList();
	}

	private void initBannerList() {
		banners = bm.getBanners();
	}

	public void preCreate() {
		current = new BannerInfo();
		current.setOrderNo(banners.size()+1);
	}

	public void save() {
		current.setCreateDate(new Date());
		if (StringUtils.isEmpty(current.getPath())) {
			addErrorMessage("请上传图片");
			return;
		}
		if (StringUtils.isEmpty(current.getId())) {
			if (StringUtils.isEmpty(current.getPath()))
				current.setId(ObjectId.get().toString());
			else
				current.setId(current.getPath().substring(0, current.getPath().indexOf(".")));
			current = bm.createOrUpdate(current);
			banners = bm.getBanners();
			
//			mobileCache.cacheChanged(MobileCache.BANNER, 1, "C", current, "id");//因暂无法判断是否修改图片，所以modified默认传1
		}else{
			current = bm.createOrUpdate(current);
			initBannerList();
			
//			mobileCache.cacheChanged(MobileCache.BANNER, 1, "U", current, "id");//因暂无法判断是否修改图片，所以modified默认传1
		}
	}

	public void delete() {
		im.delete(current);
		banners.remove(current);
		
//		mobileCache.cacheChanged(MobileCache.BANNER, 1, "D", current, "id");//因暂无法判断是否修改图片，所以modified默认传1
		
		current = null;
		addSuccessMessage("删除成功");
	}

	public void handleFileUpload(FileUploadEvent event) {
		String imageName = event.getFile().getFileName();
		current.setName(imageName);
		String suff = current.getName().substring(imageName.indexOf("."));
		if (StringUtils.isEmpty(current.getId())) 
			current.setPath(ObjectId.get().toString()+suff);
		else
			current.setPath(current.getId()+suff);
		
		boolean flage = im.create(current,event.getFile().getContents());
		if (!flage) 
			addErrorMessage("文件上传失败！");
	}
	
	public void chooseUrlNode() {
		if (selectedUrlNode == null) {
			addErrorMessage("请选择一个路径！");
			return;
		}
		String url = (String) selectedUrlNode.getData();
		StringBuilder builder = new StringBuilder(url);
		TreeNode node = selectedUrlNode.getParent();
		while (true) {
			if (node.getType().equals("root")) {
				break;
			}
			builder.insert(0, node.getData() + "/");
			node = node.getParent();
		}
		builder.insert(0, "/ui/");
		current.setLink(builder.toString());
	}
	
	public String imagePath(String path){
		return path;
	}
	
	public String formatterDate(String date){
		return date.substring(0, 10);
	}
	
	public void rowSelect(SelectEvent event) {
		current = (BannerInfo) event.getObject();
	}

	public List<BannerInfo> getBanners() {
		return banners;
	}

	public void setBanners(List<BannerInfo> banners) {
		this.banners = banners;
	}

	public BannerInfo getCurrent() {
		return current;
	}

	public void setCurrent(BannerInfo current) {
		this.current = current;
	}

	public TreeNode getSelectedUrlNode() {
		return selectedUrlNode;
	}

	public void setSelectedUrlNode(TreeNode selectedUrlNode) {
		this.selectedUrlNode = selectedUrlNode;
	}

	public String getCurrentImagePath() {
		if (null != current && !StringUtils.isEmpty(current.getPath())) {
			return current.getPath();
		}
		return "";
	}
}
